package com.example.tests;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactRemovalTests extends TestBase {
	
	@Test
	public void deleteSomeContact() {
		
		// gets contact list from created model. Initially it gets contacts from DB,
		// Then model's updated during contact creating, deleting, modification 
		SortedListOf<ContactData> contactListFromModel = app.getContactHelper().getContactsFromModel();  
		
		// get Old contact list from DB (cache)
		SortedListOf<ContactData> oldContactListFromDB = app.getContactHelper().getContactsFromCache();
	    
		// get Contact Index to remove
		int index = getRandomIndex(contactListFromModel.size());
		// or int index = getRandomIndex(oldContactListFromDB.size());
	    
	    // perform actions
	    app.getContactHelper().deleteContact(index);
		
		// save new state
	    SortedListOf<ContactData> newContactListFromUI = app.getContactHelper().getContacts();
	    
	    // get New contact list from DB (cache)
	    SortedListOf<ContactData> newContactListFromDB = app.getContactHelper().getContactsFromCache();
	    
	    // compare UI to Model 
	    assertThat(newContactListFromUI, equalTo(contactListFromModel));
	    
	    // compare UI and Cached Contact list from DB
	    assertThat(newContactListFromUI, equalTo((newContactListFromDB)));
	    
	    // compare Old and New state of Contact list from DB
	    assertThat(newContactListFromDB, equalTo((oldContactListFromDB).without(index)));
	}
}
